package cn.iocoder.yudao.module.handguard.controller.app.usertableinfo;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.app.usertableinfo.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.usertableinfo.UserTableInfoDO;
import cn.iocoder.yudao.module.handguard.convert.usertableinfo.UserTableInfoConvert;
import cn.iocoder.yudao.module.handguard.service.usertableinfo.UserTableInfoService;

@Tag(name = "管理后台 - 贷款申请基本信息、贷款申请资产信息记录")
@RestController
@RequestMapping("/hutai/user-table-info")
@Validated
public class UserTableInfoController {

    @Resource
    private UserTableInfoService userTableInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建贷款申请基本信息、贷款申请资产信息记录")
    @PreAuthorize("@ss.hasPermission('hutai:user-table-info:create')")
    public CommonResult<Long> createUserTableInfo(@Valid @RequestBody UserTableInfoCreateReqVO createReqVO) {
        return success(userTableInfoService.createUserTableInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新贷款申请基本信息、贷款申请资产信息记录")
    @PreAuthorize("@ss.hasPermission('hutai:user-table-info:update')")
    public CommonResult<Boolean> updateUserTableInfo(@Valid @RequestBody UserTableInfoUpdateReqVO updateReqVO) {
        userTableInfoService.updateUserTableInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除贷款申请基本信息、贷款申请资产信息记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('hutai:user-table-info:delete')")
    public CommonResult<Boolean> deleteUserTableInfo(@RequestParam("id") Long id) {
        userTableInfoService.deleteUserTableInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得贷款申请基本信息、贷款申请资产信息记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('hutai:user-table-info:query')")
    public CommonResult<UserTableInfoRespVO> getUserTableInfo(@RequestParam("id") Long id) {
        UserTableInfoDO userTableInfo = userTableInfoService.getUserTableInfo(id);
        return success(UserTableInfoConvert.INSTANCE.convert(userTableInfo));
    }

    @GetMapping("/list")
    @Operation(summary = "获得贷款申请基本信息、贷款申请资产信息记录列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('hutai:user-table-info:query')")
    public CommonResult<List<UserTableInfoRespVO>> getUserTableInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<UserTableInfoDO> list = userTableInfoService.getUserTableInfoList(ids);
        return success(UserTableInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得贷款申请基本信息、贷款申请资产信息记录分页")
    @PreAuthorize("@ss.hasPermission('hutai:user-table-info:query')")
    public CommonResult<PageResult<UserTableInfoRespVO>> getUserTableInfoPage(@Valid UserTableInfoPageReqVO pageVO) {
        PageResult<UserTableInfoDO> pageResult = userTableInfoService.getUserTableInfoPage(pageVO);
        return success(UserTableInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出贷款申请基本信息、贷款申请资产信息记录 Excel")
    @PreAuthorize("@ss.hasPermission('hutai:user-table-info:export')")
    @OperateLog(type = EXPORT)
    public void exportUserTableInfoExcel(@Valid UserTableInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<UserTableInfoDO> list = userTableInfoService.getUserTableInfoList(exportReqVO);
        // 导出 Excel
        List<UserTableInfoExcelVO> datas = UserTableInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "贷款申请基本信息、贷款申请资产信息记录.xls", "数据", UserTableInfoExcelVO.class, datas);
    }

}
